﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;
using Microhard.WarehouseInsp.Buz.Models;
using Microhard.WarehouseInsp.Buz.DataAccess;
using Microhard.WarehouseInsp.Buz.JHWorkflow;
using Microhard.WarehouseInsp.Buz.BuzEnum;

namespace Microhard.WarehouseInsp.Buz.DTO
{
    public static class EntityModelMappingExtension
    {
        #region SupProject
        public static SupProjectModel ToModel(this SupProject entity)
        {
            var model = Mapper.Map<SupProjectModel>(entity);
            //法人
            var fr = entity.CustomerContracts.Where(w => w.ContractType == (int)BrowserType.FR);
            if (fr.Count() > 0)
            {
                model.MortgagorFRCellphone = fr.First().Cellphone;
                model.MortgagorFRIdentityNo = fr.First().IdentityNo;
                model.MortgagorFRName = fr.First().Name;
            }
            //联系人
            var cnt = entity.CustomerContracts.Where(w => w.ContractType == (int)BrowserType.Contract);
            if (cnt.Count() > 0)
            {
                model.MortgagorContractorCellphone = cnt.First().Cellphone;
                model.MortgagorContractorName = cnt.First().Name;
            }
            return model;
        }

        public static SupProject ToEntity(this SupProjectModel model)
        {
            return Mapper.Map<SupProject>(model);
        }
        #endregion

        #region SupervisionDetail
        public static SupervisionDetailModel ToModel(this SupervisionDetail entity)
        {
            var model = Mapper.Map<SupervisionDetailModel>(entity);
            return model;
        }

        public static SupervisionDetail ToEntity(this SupervisionDetailModel model)
        {
            return Mapper.Map<SupervisionDetail>(model);
        }
        #endregion

        #region Customer
        public static CustomerModel ToModel(this JHBJ_Crm_Customer entity)
        {
            return Mapper.Map<CustomerModel>(entity);
        }

        public static JHBJ_Crm_Customer ToEntity(this CustomerModel model)
        {
            return Mapper.Map<JHBJ_Crm_Customer>(model);
        }
        #endregion

        #region PledgePlanInRecord
        public static PledgePlanInRecordsModel ToModel(this PledgePlanInRecord entity)
        {
            var model = Mapper.Map<PledgePlanInRecordsModel>(entity);
            model.ProductIDForSelect = entity.ProductList.Name;
            model.WarehouseIDForSelect = entity.Warehouse.Name;
            model.Product = entity.ProductList.ToModel();
            model.Warehouse = entity.Warehouse.ToModel();
            model.Project = entity.SupProject.ToModel();
            return model;
        }

        public static PledgePlanInRecord ToEntity(this PledgePlanInRecordsModel model)
        {
            return Mapper.Map<PledgePlanInRecord>(model);
        }
        #endregion

        #region ProductList
        public static ProductListModel ToModel(this ProductList entity)
        {
            return Mapper.Map<ProductListModel>(entity);
        }

        public static ProductList ToEntity(this ProductListModel model)
        {
            return Mapper.Map<ProductList>(model);
        }
        #endregion

        #region Warehouse
        public static WarehouseModel ToModel(this Warehouse entity)
        {
            return Mapper.Map<WarehouseModel>(entity);
        }

        public static Warehouse ToEntity(this WarehouseModel model)
        {
            return Mapper.Map<Warehouse>(model);
        }
        #endregion

        #region Attachment
        public static AttachmentModel ToModel(this Attachment entity)
        {
            return Mapper.Map<AttachmentModel>(entity);
        }

        public static Attachment ToEntity(this AttachmentModel model)
        {
            return Mapper.Map<Attachment>(model);
        }
        #endregion

        #region WarehourseTransition
        public static WarehouseTransitionModel ToModel(this WarehourseTransition entity)
        {
            return Mapper.Map<WarehouseTransitionModel>(entity);
        }

        public static WarehourseTransition ToEntity(this WarehouseTransitionModel model)
        {
            return Mapper.Map<WarehourseTransition>(model);
        }
        #endregion

        #region InspectionRecord
        public static InspectionRecordModel ToModel(this  InspectionRecord entity)
        {
            return Mapper.Map<InspectionRecordModel>(entity);
        }

        public static InspectionRecord ToEntity(this  InspectionRecordModel model)
        {
            return Mapper.Map<InspectionRecord>(model);
        }
        #endregion

        #region    RiskRecords
        public static RiskRecordsModel ToModel(this   RiskRecord entity)
        {

            var v = Mapper.Map<RiskRecordsModel>(entity);
            v.ProjectName = entity.SupProject.Name;
            return Mapper.Map<RiskRecordsModel>(v);
        }

        public static RiskRecord ToEntity(this  RiskRecordsModel model)
        {
            return Mapper.Map<RiskRecord>(model);
        }
        #endregion

        #region    ApproveRecords
        public static ApproveRecordModel ToModel(this   ApproveRecord entity)
        {
            return Mapper.Map<ApproveRecordModel>(entity);
        }

        public static ApproveRecord ToEntity(this  ApproveRecordModel model)
        {
            return Mapper.Map<ApproveRecord>(model);
        }
        #endregion


        #region PledgeChangeRecordModel
        public static PledgeChangeRecord ToEntity(this PledgeChangeRecordModel model)
        {
            return Mapper.Map<PledgeChangeRecord>(model);
        }

        public static PledgeChangeRecordModel ToModel(this PledgeChangeRecord entity)
        {
            var model = Mapper.Map<PledgeChangeRecordModel>(entity);
            model.PlgName = entity.PledgePlanInRecord.ProductList.Name;
            model.PlgCategory = entity.PledgePlanInRecord.PlgCategory;
            model.PrjCode = entity.PledgePlanInRecord.SupProject.Code;
            model.PrjName = entity.PledgePlanInRecord.SupProject.Name;
            model.WarehouseName = entity.PledgePlanInRecord.Warehouse.Name;
            model.ProtalName = entity.PledgePlanInRecord.Warehouse.WarehouseMngName;
            return model;
        }

        #endregion

        //Model 和 EF 加上影身
        #region OutInPutWarehouseRecords


        public static OutIntPutWarehouseModel ToModel(this OutInPutWarehouseRecord entity)
        {
            var model = Mapper.Map<OutIntPutWarehouseModel>(entity);
            model.PPInRecord = entity.PledgePlanInRecord.ToModel();
            model.Project = entity.PledgePlanInRecord.SupProject.ToModel();
            model.WarehouseName = entity.PledgePlanInRecord.Warehouse.Name;
            model.ProductIDForSelect = entity.PledgePlanInRecord.ProductList.Name;
            model.InProductIDForSelect = entity.PledgePlanInRecord.ProductList.Name;
            model.ProductName = entity.PledgePlanInRecord.ProductList.Name;
            model.WarehouseMngName = entity.PledgePlanInRecord.Warehouse.WarehouseMngName;
            model.ProjectName = entity.PledgePlanInRecord.SupProject.Name;
            return model;


        }

        public static OutInPutWarehouseRecord ToEntity(this OutIntPutWarehouseModel model)
        {
            return Mapper.Map<OutInPutWarehouseRecord>(model);

        }
        #endregion


        public static OutInPutApply ToEntity(this OutInPutApplyModel model)
        {
            return Mapper.Map<OutInPutApply>(model);
        }

        public static OutInPutApplyModel ToModel(this OutInPutApply entity)
        {
            var model = Mapper.Map<OutInPutApplyModel>(entity);

            model.project = entity.SupProject.ToModel();

            if (model.project != null && (string.IsNullOrEmpty(model.PrjName) || string.IsNullOrEmpty(model.PrjID.ToString())))
            {
                model.PrjID = model.project.ProjectID;
                model.PrjName = model.project.Name;


            }
            return model;

        }


    }
}
